package com.itbaizhan.rocketmqproducer.producer;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

/*意向发送消息，这种方式主要用在不特别关心发送结果的场景，例如日志发送*/
public class OnewayProducer {
    public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, RemotingException, InterruptedException {
        //创建生产者实例
        DefaultMQProducer producer = new DefaultMQProducer("producer_grp_01");
        //设置nameserver 地址
        producer.setNamesrvAddr("192.168.66.100:9876");
        //启动生产者
        producer.start();

        //创建消息并发送
        for (int i = 0; i <20; i++) {
            //注意导入的类有rocketmq
            Message message = new Message("topic_springboot_01","TagA",
                    ("hello rocketmq oneway message...").getBytes(RemotingHelper.DEFAULT_CHARSET));
            //调用sendOneway()
            producer.sendOneway(message);
        }
        producer.shutdown();
    }
}
